package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.common.model.QManufacturer;
import com.neusoft.neuiotms.dcam.fixed.model.MaintenanceChange;
import com.neusoft.neuiotms.dcam.fixed.model.QMaintenance;
import com.neusoft.neuiotms.dcam.fixed.model.QMaintenanceChange;
import com.neusoft.neuiotms.dcam.fixed.model.enums.MaintenanceChangeStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedMaintenanceChangeRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedMaintenanceChangeRepositoryImpl extends AbstractRepository
        implements CustomizedMaintenanceChangeRepository {

    @Override
    public Page<MaintenanceChange> search(final Pageable pageable, final Searchable searchable) {
        final QMaintenanceChange qMaintenanceChange = QMaintenanceChange.maintenanceChange;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<MaintenanceChange> query = this.query().selectFrom(qMaintenanceChange);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QMaintenanceChange qMaintenanceChange = QMaintenanceChange.maintenanceChange;
        if (!StringUtils.isEmpty(searchable.getStrValue("code"))) {
            where.and(super.containsStr(qMaintenanceChange.code, searchable, "code"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("changeStatus"))) {
            where.and(
                    super.equalsEnum(qMaintenanceChange.changeStatus, searchable, "changeStatus",
                            value -> MaintenanceChangeStatus.valueOf(value)));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("createTimeBegin"))
                && !StringUtils.isEmpty(searchable.getStrValue("createTimeEnd"))) {
            where.and(
                    super.afterOrEqualsDate(qMaintenanceChange.createTime, searchable,
                            "createTimeBegin"));
            where.and(super.beforeOrEqualsDate(qMaintenanceChange.createTime, searchable,
                    "createTimeEnd"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("updateTimeBegin"))
                && !StringUtils.isEmpty(searchable.getStrValue("updateTimeEnd"))) {
            where.and(
                    super.afterOrEqualsDate(qMaintenanceChange.updateTime, searchable,
                            "updateTimeBegin"));
            where.and(super.beforeOrEqualsDate(qMaintenanceChange.updateTime, searchable,
                    "updateTimeEnd"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("globalSearch"))) {
            final BooleanBuilder whereGlobal = new BooleanBuilder();
            whereGlobal.or(super.containsStr(qMaintenanceChange.code, searchable, "globalSearch"));
            for (final MaintenanceChangeStatus status : MaintenanceChangeStatus.values()) {
                int i = 0;
                if (status.getDesc().contains(searchable.getStrValue("globalSearch"))) {
                    searchable.put("status" + i, status.getCode());
                    whereGlobal.or((super.equalsEnum(qMaintenanceChange.changeStatus, searchable, "status" + i++,
                            value -> MaintenanceChangeStatus.valueOf(value))));
                }
            }

            final QMaintenance qMaintenance = qMaintenanceChange.maintenance;
            final QManufacturer qManufacturer = qMaintenanceChange.manufacturer;

            final JPQLQuery<Long> reQuery = JPAExpressions.select(qMaintenanceChange.id).from(qMaintenanceChange)
                    .leftJoin(qMaintenance).on(qMaintenanceChange.maintenance.id.eq(qMaintenance.id))
                    .leftJoin(qManufacturer).on(qMaintenanceChange.manufacturer.id.eq(qManufacturer.id));

            final BooleanBuilder likeWhere =
                    new BooleanBuilder()
                            .or(super.containsStr(qMaintenance.maintenanceNote, searchable, "globalSearch"))
                            .or(super.containsStr(qMaintenance.maintenanceUserName, searchable, "globalSearch"))
                            .or(super.containsStr(qMaintenance.maintenanceNote, searchable, "globalSearch"))
                            .or(super.containsStr(qManufacturer.name, searchable, "globalSearch"))
                            .or(super.containsStr(qManufacturer.phone, searchable, "globalSearch"))
                            .or(super.containsStr(qManufacturer.person, searchable, "globalSearch"));
            reQuery.where(likeWhere);
            whereGlobal.or(qMaintenanceChange.id.in(reQuery));
            where.and(whereGlobal);
        }
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return MaintenanceChange.class;
    }

}
